package com.ag.improvedjava.iterable;

import java.util.Iterator;
import java.util.NoSuchElementException;

final class SkipIterable<T> implements Iterable<T> {

	private final Iterable<T> iterable;
	private final int count;
	
	SkipIterable(Iterable<T> iterable, int count) {
		this.iterable = iterable;
		this.count = count;
	}

	@Override
	public Iterator<T> iterator() {
		return new SkipIterator<>(iterable.iterator(), count);
	}
}

class SkipIterator<T> extends UnmodifiableIterator<T> {
	
	private final Iterator<T> iterator;
	private int count;

	SkipIterator(Iterator<T> iterator, int count) {
		this.iterator = iterator;
		this.count = count;
	}

	@Override
	public boolean hasNext() {
		while (count > 0 && iterator.hasNext()) {
			count--;
			iterator.next();
		}
		return iterator.hasNext();
	}

	@Override
	public T next() {
		if (!hasNext()) {
			throw new NoSuchElementException();
		}
		return iterator.next();
	}

}
